from fastapi import APIRouter, HTTPException
from .models import OutSpeed, OutChange, InSpeed, InChange
from .sub_utils import FanControl
import asyncio
import time

router = APIRouter(
    prefix="/FanControl",
    tags=["风扇控制"]
)

dbFan = FanControl()


@router.post("/Change", response_model=OutChange)
async def fanChange(change: InChange):
    if type(change.status) is not bool:
        raise HTTPException(status_code=412, detail='Api data handle fault!')
    await dbFan.setOpenStatus(change.status)
    return {'status': dbFan.getOpenStatus(), 'time': time.time() * 1000}


@router.post("/Speed", response_model=OutSpeed)
async def curtainSpeed(speed: InSpeed):
    if speed.mode not in ['快', '中', '慢']:
        raise HTTPException(status_code=412, detail='Api data handle fault!')
    await dbFan.setExtraSpeed(speed.mode)
    return {'mode': dbFan.getExtraSpeed(), 'time': time.time() * 1000}